
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Marwen
 */
public class MiseAjourAction extends javax.swing.JFrame {

    
    
    int [] portefeuille;
    String ValAncien;
    String ValNouvelle;
    String id_action ;
    
    
    /**
     * Creates new form MiseAjourAction
     */
    public MiseAjourAction() {
        initComponents();
        DbManip.remplirComboBoxAUnChamps(ChoixAction, "entreprise where NOM_entreprise !='AUTRE'", "NOM_entreprise  ", "Choisir une action");
      //  ValeurAncienne.setEditable(false);
        ValeurAncienne.setEnabled(false);
        this.setTitle("Mise à jour d'une action");

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Valider = new javax.swing.JButton();
        Retour = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        ValeurAncienne = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        ChoixAction = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        ValeurNouvelle = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Valider.setBackground(new java.awt.Color(255, 255, 255));
        Valider.setText("Valider");
        Valider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ValiderActionPerformed(evt);
            }
        });

        Retour.setBackground(new java.awt.Color(255, 255, 255));
        Retour.setText("Annuler");
        Retour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RetourActionPerformed(evt);
            }
        });

        jSeparator1.setBackground(new java.awt.Color(0, 153, 153));
        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        ValeurAncienne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ValeurAncienneActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N
        jLabel3.setText("Ancienne Valeur");

        ChoixAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChoixActionActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N
        jLabel4.setText("Nouvelle Valeur ");

        ValeurNouvelle.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ValeurNouvelleKeyTyped(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N
        jLabel2.setText("Action");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ValeurNouvelle, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ValeurAncienne, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ChoixAction, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ChoixAction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ValeurAncienne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ValeurNouvelle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(25, 25, 25))
        );

        jSeparator2.setBackground(new java.awt.Color(0, 153, 153));
        jSeparator2.setForeground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(Valider, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(83, 83, 83)
                .addComponent(Retour, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(187, 187, 187))
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 566, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 566, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(46, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(168, 168, 168))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Retour)
                    .addComponent(Valider))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ValeurAncienneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ValeurAncienneActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ValeurAncienneActionPerformed

    private void ValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ValiderActionPerformed
        try {
            if("-- Choisir une action--".equals(id_action)){
                        JOptionPane.showMessageDialog(null, "Veuillez choisir une action ", "Erreur", JOptionPane.ERROR_MESSAGE);    
                        return;
            }     
            
            ValNouvelle=ValeurNouvelle.getText().toString().replaceAll(" ", "");
            if( !ValNouvelle.matches(DbManip.regexFloat))
            {
                    JOptionPane.showMessageDialog(null, "Vérifiez la valeur entrée", "Erreur", JOptionPane.ERROR_MESSAGE);
            }
            
            if(JOptionPane.showConfirmDialog(null, "Voulez-vous continuer ?", "Confirmation ",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE)!=0){
                return;
            }
            //mettre a jour la valeur actuelle ds la table action
           DbManip.requete = "update action set VALEURACTUELLE='"+ValNouvelle+"'  where id_action='" + id_action + "' ";
            DbManip.executerUpdate(DbManip.requete);        
            // ajouter une ligne ds la table historique action
            DbManip.requete = "insert into HISTORIQUE_action VALUES ('" +ValNouvelle+"','"+DbManip.date+"','"+ DbManip.next_Id("HISTORIQUE_action", "ID_HIS") + "','" + id_action + "')";
            DbManip.executerUpdate(DbManip.requete);
            // Mettre a jour les portefeuilles ayant cette action
            DbManip.requete="select ID_PORTE_FEUILLE from PORTE_FEUILLE_ACTION where id_action ='"+id_action+"'";
            DbManip.executer(DbManip.requete);
           
            DbManip.rs.last();
            int n = DbManip.rs.getRow();
            
            DbManip.rs.beforeFirst();
            portefeuille = new int [n];
            int i=0;
            while(DbManip.rs.next())
            {
                portefeuille[i]=DbManip.rs.getInt(1);
                i++;
            }
            
            Float montant;
            for(int j=0;j<portefeuille.length;j++){
                montant = DbManip.ValeurPortefeuille(String.valueOf(portefeuille[j]));
                String value = String.valueOf(montant);
                value = value.replace(".", ",");
                DbManip.requete = "insert into HISTORIQUE_PORTEFEUILLE VALUES ('" + DbManip.next_Id("HISTORIQUE_PORTEFEUILLE", "ID_HPF") + "','" + String.valueOf(portefeuille[j]) + "','" + value + "','" + DbManip.date + "')";
                DbManip.executerUpdate(DbManip.requete);
            } 
            JOptionPane.showMessageDialog(null, "Cette mise à jour a été effectuée avec succès !", "Information", JOptionPane.INFORMATION_MESSAGE);       

            
            // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(MiseAjourAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ValiderActionPerformed

    private void ChoixActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChoixActionActionPerformed
        try {
            id_action = ChoixAction.getSelectedItem().toString();
            if("-- Choisir une action--".equals(id_action))
            {
                ValeurAncienne.setText("");
                return;
            }
            // recuperer id entreprise = id action
            
            id_action = String.valueOf(DbManip.recupererIdDUnchamp("entreprise", "id_entreprise", "NOM_ENTREPRISE", id_action));
            

            DbManip.requete="select VALEURACTUELLE from action where id_action='"+id_action+"'";
            DbManip.executer(DbManip.requete);
            while(DbManip.rs.next())
            {
                ValeurAncienne.setText(String.valueOf(DbManip.rs.getInt(1)));
            }
            
            
            // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(MiseAjourAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_ChoixActionActionPerformed

    private void ValeurNouvelleKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ValeurNouvelleKeyTyped
            if ( ValeurNouvelle.getText().length() >= 12 )
        	evt.consume();
        // TODO add your handling code here:
    }//GEN-LAST:event_ValeurNouvelleKeyTyped

    private void RetourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RetourActionPerformed

        ValeurAncienne.setText("");
        ValeurNouvelle.setText("");
        ChoixAction.setSelectedIndex(0);
        
        // TODO add your handling code here:
    }//GEN-LAST:event_RetourActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MiseAjourAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MiseAjourAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MiseAjourAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MiseAjourAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MiseAjourAction().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ChoixAction;
    private javax.swing.JButton Retour;
    private javax.swing.JTextField ValeurAncienne;
    private javax.swing.JTextField ValeurNouvelle;
    private javax.swing.JButton Valider;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables
}
